package main;

import domain.Book;
import domain.Reader;
import domain.Teacher;
import service.IBookService;
import service.IReaderService;
import service.ITeacherService;
import service.impl.BookService;
import service.impl.ReaderService;
import service.impl.TeacherService;
import swing.BackgroundImage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.List;

/*
 *图书管理系统的主要入口类
 */
public class BookMngMain {
    public static void main(String[] args) {

        /*
        系统登录
        */
        teacherLogin();

    }




    public static void teacherLogin() {


        JFrame loginFrame = new JFrame("请您登录");




        JLabel labelLogin = new JLabel("登  录");

        labelLogin.setFont(new Font("宋体", Font.BOLD, 30));
        labelLogin.setBounds(220, 30, 180, 80);
        loginFrame.add(labelLogin);//将标签加入到框架中

        //添加图标
        Icon icon = new ImageIcon("D:\\idea\\bookManaged\\src\\main\\teacher.png");

        JLabel labelUsername = new JLabel("用户名");
        //图标加入
        labelUsername.setIcon(icon);
        labelUsername.setFont(new Font("宋体", Font.BOLD, 20));
        labelUsername.setBounds(90, 80, 100, 80);
        loginFrame.add(labelUsername);

        JTextField fieldUsername = new JTextField(60);
        fieldUsername.setBounds(200, 105, 150, 30);
        loginFrame.add(fieldUsername);

        Icon icon2 = new ImageIcon("D:\\idea\\bookManaged\\src\\main\\sock2.png");

        JLabel labelpwd = new JLabel("密  码");
        labelpwd.setIcon(icon2);
        labelpwd.setFont(new Font("宋体", Font.BOLD, 20));
        labelpwd.setBounds(90, 135, 100, 80);
        loginFrame.add(labelpwd);

        JPasswordField fieldPwd = new JPasswordField(60);
        fieldPwd.setBounds(200, 155, 150, 30);
        loginFrame.add(fieldPwd);


        JButton buttonLogin = new JButton("登 录");
        buttonLogin.setBounds(180, 200, 80, 40);

        JButton buttonclose = new JButton("退  出 ");
        buttonclose.setBounds(300, 200, 80, 40);




        buttonclose.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                loginFrame.dispose();
                loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        });

        buttonLogin.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //获取输入框的值
                String workno = fieldUsername.getText();
                String pwd = fieldPwd.getText();
                //接受输入的参数
                ITeacherService teacherService = new TeacherService();
                boolean flag = teacherService.login(workno, pwd);

                IReaderService readerService = new ReaderService();
                boolean flag2 = readerService.login(workno, pwd);

                if (!flag&&!flag2) {
                    JOptionPane.showMessageDialog(loginFrame, "用户名或者密码错误");

                    return;

                } else if(flag2){
                    JOptionPane.showMessageDialog(loginFrame, "恭喜读者登陆成功");
                    loginFrame.dispose();

                    JFrame showBooksFrame2 = new JFrame("功能");

                    JPanel p_1 = new JPanel(new BorderLayout());
                    JTabbedPane tabbedPane = new JTabbedPane();


                    JToolBar toolBar1 = new JToolBar();
                    toolBar1.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 10));

                    JToolBar toolBar2 = new JToolBar();
                    toolBar2.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 10));

                    JToolBar toolBar3 = new JToolBar();
                    toolBar3.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 10));


                    tabbedPane.addTab("图书功能", toolBar1);
                    Icon icon20 = new ImageIcon("D:\\idea\\bookManaged\\src\\image\\search5.png");
                    JButton button11 = new JButton("查询图书");
                    button11.setIcon(icon20);
                    Icon icon21 = new ImageIcon("D:\\idea\\bookManaged\\src\\image\\borrowbook.png");
                    JButton button8 = new JButton("借阅图书");
                    button8.setIcon(icon21);
                    Icon icon22 = new ImageIcon("D:\\idea\\bookManaged\\src\\image\\returnbook.png");
                    JButton button9 = new JButton("归还图书");
                    button9.setIcon(icon21);


                    toolBar1.add(button8);
                    toolBar1.add(button9);
                    toolBar1.add(button11);

                    tabbedPane.addTab("罚款功能", toolBar2);
                    JButton button1 = new JButton("查询罚款");
                    JButton button2 = new JButton("缴纳罚款");
                    toolBar2.add(button1);
                    toolBar2.add(button2);



                    tabbedPane.addTab("借阅历史", toolBar3);
                    JButton vertical = new JButton("查看天数");
                    toolBar3.add(vertical);

                    showBooksFrame2.getContentPane().add(tabbedPane, BorderLayout.NORTH);
                    showBooksFrame2.getContentPane().add(p_1, BorderLayout.CENTER);
                    showBooksFrame2.setLocation(500, 180);//设置frame框在屏幕上的显示位置
                    showBooksFrame2.setSize(800, 500);


                    // 设置默认选中的选项卡
                    tabbedPane.setSelectedIndex(1);
                    showBooksFrame2.setVisible(true);

                    //归还图书
                    button9.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            JFrame returnBookFrame = new JFrame("归还图书");

                            JLabel bookNum = new JLabel("书的编号");
                            bookNum.setFont(new Font("宋体", Font.BOLD, 20));
                            bookNum.setBounds(180, 30, 150, 50);
                            returnBookFrame.add(bookNum);

                            JTextField num = new JTextField(60);
                            num.setBounds(180, 80, 100, 30);
                            returnBookFrame.add(num);

                            JButton returnBook = new JButton("归还");
                            returnBook.setBounds(190, 120, 80, 40);

                            returnBook.addMouseListener(new MouseAdapter() {
                                public void mouseClicked(MouseEvent e) {
                                    //anum为书的编号
                                    String anum = num.getText();
                                    String astate = "在馆";
                                    String borrowbook = "null";


                                    BookService bookService = new BookService();
                                    Book book = new Book(anum, astate);
                                    ReaderService readerService = new ReaderService();
                                    Reader reader = new Reader(workno,borrowbook);
                                    try {
                                        int result = bookService.returnBookState(book);
                                        int result2 = readerService.returnBorrowbook(reader);

                                        JOptionPane.showMessageDialog(returnBookFrame, "还书成功");
                                    } catch (SQLException e1) {
                                        e1.printStackTrace();
                                    }
                                }
                            });

                            returnBookFrame.setResizable(false);
                            returnBookFrame.add(returnBook);
                            returnBookFrame.setIconImage(Toolkit.getDefaultToolkit().createImage(SwingTest.class.getClassLoader().getResource("main/bookTitle.png")));
                            returnBookFrame.setLayout(null);
                            returnBookFrame.setLocation(700, 300);
                            returnBookFrame.setSize(480, 200);
                            returnBookFrame.setVisible(true);
                        }
                    });

                    //借阅图书
                    button8.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            JFrame borrowBookFrame = new JFrame("借阅图书");

                            JLabel bookNum = new JLabel("书的编号");
                            bookNum.setFont(new Font("宋体", Font.BOLD, 20));
                            bookNum.setBounds(180, 30, 150, 50);
                            borrowBookFrame.add(bookNum);

                            JTextField num = new JTextField(60);
                            num.setBounds(180, 80, 100, 30);
                            borrowBookFrame.add(num);

                            JButton updateBook = new JButton("借阅");
                            updateBook.setBounds(190, 120, 80, 40);

                            updateBook.addMouseListener(new MouseAdapter() {
                                public void mouseClicked(MouseEvent e) {
                                    //anum为书的编号
                                    String anum = num.getText();
                                    String astate = "已借出";

                                    BookService bookService = new BookService();
                                    Book book = new Book(anum, astate);
                                    ReaderService readerService = new ReaderService();
                                    Reader reader = new Reader(workno,anum);
                                    try {
                                        int result = bookService.updateBookState(book);
                                        int result2 = readerService.updateBorrowbook(reader);

                                        JOptionPane.showMessageDialog(borrowBookFrame, "借阅成功");
                                    } catch (SQLException e1) {
                                        e1.printStackTrace();
                                    }
                                }
                            });

                            borrowBookFrame.setResizable(false);
                            borrowBookFrame.add(updateBook);
                            borrowBookFrame.setIconImage(Toolkit.getDefaultToolkit().createImage(SwingTest.class.getClassLoader().getResource("main/bookTitle.png")));
                            borrowBookFrame.setLayout(null);
                            borrowBookFrame.setLocation(700, 300);
                            borrowBookFrame.setSize(480, 200);
                            borrowBookFrame.setVisible(true);
                        }
                    });


                            //查询图书
                            button11.addMouseListener(new MouseAdapter() {
                                                          @Override
                                                          public void mouseClicked(MouseEvent e) {
                                                              String[] header = {"书名", "编号", "价钱", "作者", "状态", "出版社", "说明"};

                                                              IBookService bookService = new BookService();
                                                              List books = bookService.selectBooks();

                                                              String[][] rowData = new String[books.size()][7];
                                                              for (int i = 0; i < books.size(); i++) {
                                                                  Book book = (Book) books.get(i);
                                                                  rowData[i][0] = book.getName();
                                                                  rowData[i][1] = book.getBookno();
                                                                  rowData[i][2] = book.getPrivce();
                                                                  rowData[i][3] = book.getAuth();
                                                                  rowData[i][4] = book.getState();
                                                                  rowData[i][5] = book.getCbs();
                                                                  rowData[i][6] = book.getContent();
                                                              }
                                                              JTable table = new JTable(rowData, header);
                                                              JScrollPane scrollPane = new JScrollPane(table);

                                                              showBooksFrame2.add(scrollPane);
                                                              showBooksFrame2.setIconImage(Toolkit.getDefaultToolkit().createImage(SwingTest.class.getClassLoader().getResource("main/bookTitle.png")));
                                                              showBooksFrame2.setLocation(500, 180);//设置frame框在屏幕上的显示位置
                                                              showBooksFrame2.setSize(800, 500);
                                                              /*BackgroundImage2 backgroundImage = new BackgroundImage2();
                                                              showBooksFrame2.add(backgroundImage);*/
                                                              showBooksFrame2.setVisible(true);


                                                          }
                                                      }

                            );

                        }
                else if(flag) {

                    JOptionPane.showMessageDialog(loginFrame, "恭喜管理员登陆成功");
                    loginFrame.dispose();
                    //展示图书
                    JFrame showBooksFrame = new JFrame("功能");
                    JPanel p_1 = new JPanel(new BorderLayout());
                    JTabbedPane tabbedPane = new JTabbedPane();


                    JToolBar toolBar1 = new JToolBar();
                    toolBar1.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 10));

                    JToolBar toolBar2 = new JToolBar();
                    toolBar2.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 10));

                    JToolBar toolBar3 = new JToolBar();
                    toolBar3.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 10));
                    //hgap代表水平间距，vgap代表垂直间距


                    tabbedPane.addTab("图书维护", toolBar1);
                    Icon icon4 = new ImageIcon("D:\\idea\\bookManaged\\src\\image\\search5.png");
                    JButton button11 = new JButton("查询图书");
                    button11.setIcon(icon4);
                    Icon icon6 = new ImageIcon("D:\\idea\\bookManaged\\src\\image\\add3.png");
                    JButton button8 = new JButton("增加图书");
                    button8.setIcon(icon6);
                    Icon icon5 = new ImageIcon("D:\\idea\\bookManaged\\src\\image\\delete3.png");
                    JButton button9 = new JButton("删除图书");
                    button9.setIcon(icon5);
                    Icon icon3 = new ImageIcon("D:\\idea\\bookManaged\\src\\image\\set2.png");
                    JButton button10 = new JButton("修改图书");
                    button10.setIcon(icon3);


                    toolBar1.add(button8);
                    toolBar1.add(button9);
                    toolBar1.add(button10);
                    toolBar1.add(button11);

                    tabbedPane.addTab("人员管理", toolBar2);
                    Icon icon10 = new ImageIcon("D:\\idea\\bookManaged\\src\\image\\addm2.png");
                    JButton button1 = new JButton("添加管理员");
                    button1.setIcon(icon10);
                    Icon icon11 = new ImageIcon("D:\\idea\\bookManaged\\src\\image\\delete3.png");
                    JButton button2 = new JButton("删除管理员");
                    button2.setIcon(icon11);
                    Icon icon12 = new ImageIcon("D:\\idea\\bookManaged\\src\\image\\search5.png");
                    JButton button3 = new JButton("查询管理员");
                    button3.setIcon(icon12);
                    Icon icon13 = new ImageIcon("D:\\idea\\bookManaged\\src\\image\\search5.png");
                    JButton button15 = new JButton("查询读者");
                    button15.setIcon(icon13);
                    Icon icon15 = new ImageIcon("D:\\idea\\bookManaged\\src\\image\\addm2.png");
                    JButton button16 = new JButton("添加读者");
                    button16.setIcon(icon15);
                    Icon icon14 = new ImageIcon("D:\\idea\\bookManaged\\src\\image\\delete3.png");
                    JButton button17 = new JButton("删除读者");
                    button17.setIcon(icon14);

                    toolBar2.add(button1);
                    toolBar2.add(button2);
                    toolBar2.add(button3);
                    toolBar2.add(button15);
                    toolBar2.add(button16);
                    toolBar2.add(button17);

                    tabbedPane.addTab("罚款记录", toolBar3);
                    JButton vertical = new JButton("正常借书读者");
                    JButton horizontal = new JButton("超期借书读者");
                    toolBar3.add(vertical);
                    toolBar3.add(horizontal);

                    showBooksFrame.getContentPane().add(tabbedPane, BorderLayout.NORTH);
                    showBooksFrame.getContentPane().add(p_1, BorderLayout.CENTER);
                    showBooksFrame.setLocation(500, 180);//设置frame框在屏幕上的显示位置
                    showBooksFrame.setSize(900, 500);


                    // 设置默认选中的选项卡
                    tabbedPane.setSelectedIndex(1);
                    showBooksFrame.setVisible(true);

                    //删除读者
                    button17.addMouseListener(new MouseAdapter() {
                        public void mouseClicked(MouseEvent e) {
                            JFrame deleteReaderFrame = new JFrame("删除读者");


                            JButton deleteReader = new JButton("删除");
                            deleteReader.setBounds(190, 120, 80, 40);

                            JLabel managerNum = new JLabel("读者的编号");
                            managerNum.setFont(new Font("宋体", Font.BOLD, 20));
                            managerNum.setBounds(180, 30, 150, 50);
                            deleteReaderFrame.add(managerNum);

                            JTextField num = new JTextField(60);
                            num.setBounds(180, 80, 100, 30);
                            deleteReaderFrame.add(num);

                            deleteReader.addMouseListener(new MouseAdapter() {
                                public void mouseClicked(MouseEvent e) {
                                    IReaderService readerService = new ReaderService();
                                    String deleteNum =num.getText();
                                    readerService.deleteReader(deleteNum);
                                    JOptionPane.showMessageDialog(deleteReaderFrame,"删除成功");
                                }
                            });
                            deleteReaderFrame.add(deleteReader);
                            deleteReaderFrame.setResizable(false);
                            deleteReaderFrame.setIconImage(Toolkit.getDefaultToolkit().createImage(SwingTest.class.getClassLoader().getResource("main/bookTitle.png")));
                            deleteReaderFrame.setLayout(null);
                            deleteReaderFrame.setLocation(700, 300);
                            deleteReaderFrame.setSize(480, 200);
                            deleteReaderFrame.setVisible(true);
                        }
                    });


                    //查询读者
                    button15.addMouseListener(new MouseAdapter() {
                                                 @Override
                                                 public void mouseClicked(MouseEvent e) {
                                                     String[] header = {"学号", "姓名", "性别", "学校","密码","借书编号"};

                                                     IReaderService readerService = new ReaderService();
                                                     List readers = readerService.selectReaders();


                                                     String[][] rowData = new String[readers.size()][6];
                                                     for (int i = 0; i < readers.size(); i++) {
                                                         Reader reader = (Reader )readers.get(i);
                                                         rowData[i][0] = reader.getRnum();
                                                         rowData[i][1] = reader.getRname();
                                                         rowData[i][2] = reader.getRsex();
                                                         rowData[i][3] = reader.getRschool();
                                                         rowData[i][4] = reader.getRpwd();
                                                         rowData[i][5] = reader.getBorrowbook();
                                                     }
                                                     JTable table = new JTable(rowData, header);
                                                     JScrollPane scrollPane = new JScrollPane(table);

                                                     showBooksFrame.add(scrollPane);
                                                     showBooksFrame.setIconImage(Toolkit.getDefaultToolkit().createImage(SwingTest.class.getClassLoader().getResource("main/bookTitle.png")));
                                                     showBooksFrame.setLocation(500, 180);//设置frame框在屏幕上的显示位置
                                                     showBooksFrame.setSize(900, 500);
                                                     /*BackgroundImage2 backgroundImage = new BackgroundImage2();
                                                     showBooksFrame.add(backgroundImage);*/
                                                     showBooksFrame.setVisible(true);


                                                 }
                                             }

                    );
                    //添加读者
                    button16.addMouseListener(new MouseAdapter() {
                        public void mouseClicked(MouseEvent e) {
                            JFrame addReaderFrame = new JFrame("添加读者");
                            JLabel workNo = new JLabel("学号");
                            workNo.setFont(new Font("宋体", Font.BOLD, 20));
                            workNo.setBounds(50, 10, 100, 80);
                            addReaderFrame.add(workNo);

                            JTextField num = new JTextField(60);
                            num.setBounds(50, 60, 100, 30);
                            addReaderFrame.add(num);

                            JLabel workSex = new JLabel("性别");
                            workSex.setFont(new Font("宋体", Font.BOLD, 20));
                            workSex.setBounds(190, 10, 100, 80);
                            addReaderFrame.add(workSex);

                            JTextField sex = new JTextField(60);
                            sex.setBounds(190, 60, 100, 30);
                            addReaderFrame.add(sex);

                            JLabel workCollege = new JLabel("大学");
                            workCollege.setFont(new Font("宋体", Font.BOLD, 20));
                            workCollege.setBounds(330, 10, 100, 80);
                            addReaderFrame.add(workCollege);

                            JTextField college = new JTextField(60);
                            college.setBounds(330, 60, 100, 30);
                            addReaderFrame.add(college);

                            JLabel workName = new JLabel("名字");
                            workName.setFont(new Font("宋体", Font.BOLD, 20));
                            workName.setBounds(450, 10, 150, 80);
                            addReaderFrame.add(workName);

                            JTextField name = new JTextField(60);
                            name.setBounds(450, 60, 100, 30);
                            addReaderFrame.add(name);

                            JLabel workPwd = new JLabel("密码");
                            workPwd.setFont(new Font("宋体", Font.BOLD, 20));
                            workPwd.setBounds(600, 10, 150, 80);
                            addReaderFrame.add(workPwd);

                            JTextField pwd = new JTextField(60);
                            pwd.setBounds(600, 60, 100, 30);
                            addReaderFrame.add(pwd);


                            JButton addReader = new JButton("添加");
                            addReader.setBounds(750, 55, 80, 40);
                            addReader.addMouseListener(new MouseAdapter() {
                                public void mouseClicked(MouseEvent e) {
                                    String anum = num.getText();
                                    String asex = sex.getText();
                                    String acollege = college.getText();
                                    String aname = name.getText();
                                    String apwd = pwd.getText();
                                    String aborrowbook = "null";

                                    IReaderService readerService = new ReaderService();
                                    Reader reader = new Reader(anum,aname,asex,acollege,apwd,aborrowbook);
                                    JOptionPane.showMessageDialog(addReaderFrame,"添加成功");
                                    int result = readerService.insertReader(reader);
                                }});


                            addReaderFrame.setResizable(false);
                            addReaderFrame.add(addReader);
                            addReaderFrame.setIconImage(Toolkit.getDefaultToolkit().createImage(SwingTest.class.getClassLoader().getResource("main/bookTitle.png")));
                            addReaderFrame.setLayout(null);
                            addReaderFrame.setLocation(450, 300);
                            addReaderFrame.setSize(1000, 200);
                            addReaderFrame.setVisible(true);


                        }
                    });

                            //删除管理员
                    button2.addMouseListener(new MouseAdapter() {
                        public void mouseClicked(MouseEvent e) {
                            JFrame deleteManagerFrame = new JFrame("删除管理员");


                            JButton deleteManager = new JButton("删除");
                            deleteManager.setBounds(190, 120, 80, 40);

                            JLabel managerNum = new JLabel("管理员的编号");
                            managerNum.setFont(new Font("宋体", Font.BOLD, 20));
                            managerNum.setBounds(180, 30, 150, 50);
                            deleteManagerFrame.add(managerNum);

                            JTextField num = new JTextField(60);
                            num.setBounds(180, 80, 100, 30);
                            deleteManagerFrame.add(num);

                            deleteManager.addMouseListener(new MouseAdapter() {
                                public void mouseClicked(MouseEvent e) {
                                    ITeacherService teacherService = new TeacherService();
                                    String deleteNum =num.getText();
                                    teacherService.deleteManager(deleteNum);
                                    JOptionPane.showMessageDialog(deleteManagerFrame,"删除成功");
                                }
                            });
                            deleteManagerFrame.add(deleteManager);
                            deleteManagerFrame.setResizable(false);
                            deleteManagerFrame.setIconImage(Toolkit.getDefaultToolkit().createImage(SwingTest.class.getClassLoader().getResource("main/bookTitle.png")));
                            deleteManagerFrame.setLayout(null);
                            deleteManagerFrame.setLocation(700, 300);
                            deleteManagerFrame.setSize(480, 200);
                            deleteManagerFrame.setVisible(true);
                        }
                    });




                    //查询管理员
                    button3.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            String[] header = {"工号", "性别", "大学", "姓名","密码"};

                            ITeacherService teacherService = new TeacherService();
                            List teachers = teacherService.selectManagers();


                            String[][] rowData = new String[teachers.size()][5];
                            for (int i = 0; i < teachers.size(); i++) {
                                Teacher teacher = (Teacher)teachers.get(i);
                                rowData[i][0] = teacher.getWorkno();
                                rowData[i][1] = teacher.getSex();
                                rowData[i][2] = teacher.getCollege();
                                rowData[i][3] = teacher.getName();
                                rowData[i][4] = teacher.getPwd();
                            }
                            JTable table = new JTable(rowData, header);
                            JScrollPane scrollPane = new JScrollPane(table);

                            showBooksFrame.add(scrollPane);
                            showBooksFrame.setIconImage(Toolkit.getDefaultToolkit().createImage(SwingTest.class.getClassLoader().getResource("main/bookTitle.png")));
                            showBooksFrame.setLocation(500, 180);//设置frame框在屏幕上的显示位置
                            showBooksFrame.setSize(900, 500);
                         /*   BackgroundImage2 backgroundImage = new BackgroundImage2();
                            showBooksFrame.add(backgroundImage);*/
                            showBooksFrame.setVisible(true);


                        }
                                             }

                    );






                    //添加管理员
                    button1.addMouseListener(new MouseAdapter() {
                        public void mouseClicked(MouseEvent e) {
                            JFrame addManagerFrame = new JFrame("添加管理员");

                            JLabel workNo = new JLabel("工号");
                            workNo.setFont(new Font("宋体", Font.BOLD, 20));
                            workNo.setBounds(50, 10, 100, 80);
                            addManagerFrame.add(workNo);

                            JTextField num = new JTextField(60);
                            num.setBounds(50, 60, 100, 30);
                            addManagerFrame.add(num);

                            JLabel workSex = new JLabel("性别");
                            workSex.setFont(new Font("宋体", Font.BOLD, 20));
                            workSex.setBounds(190, 10, 100, 80);
                            addManagerFrame.add(workSex);

                            JTextField sex = new JTextField(60);
                            sex.setBounds(190, 60, 100, 30);
                            addManagerFrame.add(sex);

                            JLabel workCollege = new JLabel("大学");
                            workCollege.setFont(new Font("宋体", Font.BOLD, 20));
                            workCollege.setBounds(330, 10, 100, 80);
                            addManagerFrame.add(workCollege);

                            JTextField college = new JTextField(60);
                            college.setBounds(330, 60, 100, 30);
                            addManagerFrame.add(college);

                            JLabel workName = new JLabel("名字");
                            workName.setFont(new Font("宋体", Font.BOLD, 20));
                            workName.setBounds(450, 10, 150, 80);
                            addManagerFrame.add(workName);

                            JTextField name = new JTextField(60);
                            name.setBounds(450, 60, 100, 30);
                            addManagerFrame.add(name);

                            JLabel workPwd = new JLabel("密码");
                            workPwd.setFont(new Font("宋体", Font.BOLD, 20));
                            workPwd.setBounds(600, 10, 150, 80);
                            addManagerFrame.add(workPwd);

                            JTextField pwd = new JTextField(60);
                            pwd.setBounds(600, 60, 100, 30);
                            addManagerFrame.add(pwd);


                            JButton addManager = new JButton("添加");
                            addManager.setBounds(750, 55, 80, 40);
                            addManager.addMouseListener(new MouseAdapter() {
                                public void mouseClicked(MouseEvent e) {
                                    String anum = num.getText();
                                    String asex = sex.getText();
                                    String acollege = college.getText();
                                    String aname = name.getText();
                                    String apwd = pwd.getText();

                                    ITeacherService teacherService = new TeacherService();
                                    Teacher teacher = new Teacher(anum,asex,acollege,aname,apwd);
                                    JOptionPane.showMessageDialog(addManagerFrame,"添加成功");
                                    int result = teacherService.insertManager(teacher);
                                }});


                            addManagerFrame.setResizable(false);
                            addManagerFrame.add(addManager);
                            addManagerFrame.setIconImage(Toolkit.getDefaultToolkit().createImage(SwingTest.class.getClassLoader().getResource("main/bookTitle.png")));
                            addManagerFrame.setLayout(null);
                            addManagerFrame.setLocation(450, 300);
                            addManagerFrame.setSize(1000, 200);
                            addManagerFrame.setVisible(true);


                        }
                    });



                    //修改图书
                    button10.addMouseListener(new MouseAdapter() {
                        public void mouseClicked(MouseEvent e) {
                            JFrame updateBookFrame = new JFrame("修改图书");

                            JLabel bookName = new JLabel("书的名字");
                            bookName.setFont(new Font("宋体", Font.BOLD, 20));
                            bookName.setBounds(50, 10, 100, 80);
                            updateBookFrame.add(bookName);

                            JTextField name = new JTextField(60);
                            name.setBounds(50, 60, 100, 30);
                            updateBookFrame.add(name);

                            JLabel bookNum = new JLabel("书的编号");
                            bookNum.setFont(new Font("宋体", Font.BOLD, 20));
                            bookNum.setBounds(190, 10, 100, 80);
                            updateBookFrame.add(bookNum);

                            JTextField num = new JTextField(60);
                            num.setBounds(190, 60, 100, 30);
                            updateBookFrame.add(num);

                            JLabel bookAuth = new JLabel("书的作者");
                            bookAuth.setFont(new Font("宋体", Font.BOLD, 20));
                            bookAuth.setBounds(330, 10, 100, 80);
                            updateBookFrame.add(bookAuth);

                            JTextField auth = new JTextField(60);
                            auth.setBounds(330, 60, 100, 30);
                            updateBookFrame.add(auth);

                            JLabel bookPbs = new JLabel("书的出版社");
                            bookPbs.setFont(new Font("宋体", Font.BOLD, 20));
                            bookPbs.setBounds(450, 10, 150, 80);
                            updateBookFrame.add(bookPbs);

                            JTextField Pbs = new JTextField(60);
                            Pbs.setBounds(450, 60, 100, 30);
                            updateBookFrame.add(Pbs);

                            JLabel bookPrice = new JLabel("书的价钱");
                            bookPrice.setFont(new Font("宋体", Font.BOLD, 20));
                            bookPrice.setBounds(600, 10, 150, 80);
                            updateBookFrame.add(bookPrice);

                            JTextField price = new JTextField(60);
                            price.setBounds(600, 60, 100, 30);
                            updateBookFrame.add(price);

                            JButton updateBook = new JButton("修改");
                            updateBook.setBounds(750, 55, 80, 40);

                            updateBook.addMouseListener(new MouseAdapter() {

                                public void mouseClicked(MouseEvent e) {
                                    String aname = name.getText();
                                    String anum = num.getText();
                                    String aauth = auth.getText();
                                    String aPbs = Pbs.getText();
                                    String aprice = price.getText();
                                    String astate = "在馆";
                                    String acontent = "这是一本好书";

                                    IBookService bookService = new BookService();
                                    Book book = new Book(aname, anum, aprice, aauth, aPbs, astate, acontent);
                                    try {
                                        int result = bookService.updateBook(book);
                                        JOptionPane.showMessageDialog(updateBookFrame,"修改成功");
                                    } catch (SQLException e1) {
                                        e1.printStackTrace();
                                    }
                                }});


                            updateBookFrame.setResizable(false);
                            updateBookFrame.add(updateBook);
                            updateBookFrame.setIconImage(Toolkit.getDefaultToolkit().createImage(SwingTest.class.getClassLoader().getResource("main/bookTitle.png")));
                            updateBookFrame.setLayout(null);
                            updateBookFrame.setLocation(450, 300);
                            updateBookFrame.setSize(1000, 200);
                            updateBookFrame.setVisible(true);


                        }
                    });
                    //增加图书
                    button8.addMouseListener(new MouseAdapter() {
                        public void mouseClicked(MouseEvent e) {
                            JFrame addBookFrame = new JFrame("添加图书");

                            JLabel bookName = new JLabel("书的名字");
                            bookName.setFont(new Font("宋体", Font.BOLD, 20));
                            bookName.setBounds(50, 10, 100, 80);
                            addBookFrame.add(bookName);

                            JTextField name = new JTextField(60);
                            name.setBounds(50, 60, 100, 30);
                            addBookFrame.add(name);

                            JLabel bookNum = new JLabel("书的编号");
                            bookNum.setFont(new Font("宋体", Font.BOLD, 20));
                            bookNum.setBounds(190, 10, 100, 80);
                            addBookFrame.add(bookNum);

                            JTextField num = new JTextField(60);
                            num.setBounds(190, 60, 100, 30);
                            addBookFrame.add(num);

                            JLabel bookAuth = new JLabel("书的作者");
                            bookAuth.setFont(new Font("宋体", Font.BOLD, 20));
                            bookAuth.setBounds(330, 10, 100, 80);
                            addBookFrame.add(bookAuth);

                            JTextField auth = new JTextField(60);
                            auth.setBounds(330, 60, 100, 30);
                            addBookFrame.add(auth);

                            JLabel bookPbs = new JLabel("书的出版社");
                            bookPbs.setFont(new Font("宋体", Font.BOLD, 20));
                            bookPbs.setBounds(450, 10, 150, 80);
                            addBookFrame.add(bookPbs);

                            JTextField Pbs = new JTextField(60);
                            Pbs.setBounds(450, 60, 100, 30);
                            addBookFrame.add(Pbs);

                            JLabel bookPrice = new JLabel("书的价钱");
                            bookPrice.setFont(new Font("宋体", Font.BOLD, 20));
                            bookPrice.setBounds(600, 10, 150, 80);
                            addBookFrame.add(bookPrice);

                            JTextField price = new JTextField(60);
                            price.setBounds(600, 60, 100, 30);
                            addBookFrame.add(price);


                            JButton addBook = new JButton("添加");
                            addBook.setBounds(750, 55, 80, 40);
                            addBook.addMouseListener(new MouseAdapter() {

                                public void mouseClicked(MouseEvent e) {
                                String aname = name.getText();
                                String anum = num.getText();
                                String aauth = auth.getText();
                                String aPbs = Pbs.getText();
                                String aprice = price.getText();
                                String astate = "在馆";
                                String acontent = "这是一本好书";

                                IBookService bookService = new BookService();
                                Book book = new Book(aname, anum, aprice, aauth, aPbs, astate, acontent);
                                    try {
                                        int result = bookService.insertBook(book);
                                        JOptionPane.showMessageDialog(addBookFrame,"添加成功");
                                    } catch (SQLException e1) {
                                        e1.printStackTrace();
                                    }
                                }});
                            addBookFrame.setResizable(false);
                            addBookFrame.add(addBook);
                            addBookFrame.setIconImage(Toolkit.getDefaultToolkit().createImage(SwingTest.class.getClassLoader().getResource("main/bookTitle.png")));
                            addBookFrame.setLayout(null);
                            addBookFrame.setLocation(450, 300);
                            addBookFrame.setSize(1000, 200);
                           // addBookFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                            addBookFrame.setVisible(true);


                        }
                    });
                    //删除图书
                    button9.addMouseListener(new MouseAdapter() {
                        public void mouseClicked(MouseEvent e) {
                            JFrame deleteBookFrame = new JFrame("删除图书");


                            JButton deleteBook = new JButton("删除");
                            deleteBook.setBounds(190, 120, 80, 40);

                            JLabel bookNum = new JLabel("书的编号");
                            bookNum.setFont(new Font("宋体", Font.BOLD, 20));
                            bookNum.setBounds(180, 30, 150, 50);
                            deleteBookFrame.add(bookNum);

                            JTextField num = new JTextField(60);
                            num.setBounds(180, 80, 100, 30);
                            deleteBookFrame.add(num);

                            deleteBook.addMouseListener(new MouseAdapter() {
                                public void mouseClicked(MouseEvent e) {
                                    IBookService bookService = new BookService();
                                    String deleteNum =num.getText();
                                    bookService.deleteBook(deleteNum);
                                    JOptionPane.showMessageDialog(deleteBookFrame,"删除成功");
                                }
                                });
                            deleteBookFrame.add(deleteBook);
                            deleteBookFrame.setResizable(false);
                            deleteBookFrame.setIconImage(Toolkit.getDefaultToolkit().createImage(SwingTest.class.getClassLoader().getResource("main/bookTitle.png")));
                            deleteBookFrame.setLayout(null);
                            deleteBookFrame.setLocation(700, 300);
                            deleteBookFrame.setSize(480, 200);
                            deleteBookFrame.setVisible(true);
                        }
                    });
                    //查询图书
                    button11.addMouseListener(new MouseAdapter() {
                          @Override
                          public void mouseClicked(MouseEvent e) {
                              String[] header = {"书名", "编号", "价钱", "作者","状态","出版社","说明"};

                              IBookService bookService = new BookService();
                              List books = bookService.selectBooks();

                              String[][] rowData = new String[books.size()][7];
                              for (int i = 0; i < books.size(); i++) {
                                  Book book = (Book) books.get(i);
                                  rowData[i][0] = book.getName();
                                  rowData[i][1] = book.getBookno();
                                  rowData[i][2] = book.getPrivce();
                                  rowData[i][3] = book.getAuth();
                                  rowData[i][4] = book.getState();
                                  rowData[i][5] = book.getCbs();
                                  rowData[i][6] = book.getContent();
                              }
                              JTable table = new JTable(rowData, header);
                              JScrollPane scrollPane = new JScrollPane(table);

                              showBooksFrame.add(scrollPane);
                              showBooksFrame.setIconImage(Toolkit.getDefaultToolkit().createImage(SwingTest.class.getClassLoader().getResource("main/bookTitle.png")));
                              showBooksFrame.setLocation(500, 180);//设置frame框在屏幕上的显示位置
                              showBooksFrame.setSize(900, 500);

                              /*BackgroundImage backgroundImage = new BackgroundImage();
                              showBooksFrame.add(backgroundImage);*/
                              showBooksFrame.setVisible(true);


                          }
                      }

                    );

                }
            }
        });



        loginFrame.setResizable(false);
        loginFrame.add(buttonLogin);
        loginFrame.add(buttonclose);
        loginFrame.setIconImage(Toolkit.getDefaultToolkit().createImage(SwingTest.class.getClassLoader().getResource("main/bookTitle.png")));
      //  loginFrame.setLayout(null);
        loginFrame.setLocation(600, 300);
        loginFrame.setSize(540, 340);

        loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //显示图片
        BackgroundImage backgroundImage = new BackgroundImage();
        loginFrame.add(backgroundImage);

        loginFrame.setVisible(true);
    }
}
